package com.xiaoshuidi.cloud.module.finance.service.certificate.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoshuidi.cloud.module.finance.controller.admin.certificate.vo.FinanceCertificateConfigReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.certificate.vo.FinanceCertificateConfigRespVO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.certificate.FinanceCertificateConfigDO;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.certificate.FinanceCertificateConfigMapper;
import com.xiaoshuidi.cloud.module.finance.service.certificate.FinanceCertificateConfigService;
import com.xiaoshuidi.cloud.module.system.api.apartment.ApartmentManageApi;
import com.xiaoshuidi.cloud.module.system.enums.apartment.ApartmentManageEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class FinanceCertificateConfigServiceImpl implements FinanceCertificateConfigService {

    @Resource
    private FinanceCertificateConfigMapper financeCertificateConfigMapper;
    @Resource
    private ApartmentManageApi apartmentManageApi;

    /**
     * 获取配置信息
     *
     * @param apartmentId
     * @return
     */
    @Override
    public List<FinanceCertificateConfigRespVO> get(Long apartmentId) {
        List<FinanceCertificateConfigDO> certificateList = financeCertificateConfigMapper.selectList(FinanceCertificateConfigDO::getApartmentId, apartmentId);
        if (null != certificateList && certificateList.size() > 0) {

            List<FinanceCertificateConfigRespVO> certificateProjectList = new ArrayList<>();
            for (FinanceCertificateConfigDO configDO : certificateList) {
                FinanceCertificateConfigRespVO vo = new FinanceCertificateConfigRespVO();
                vo.setApartmentId(configDO.getApartmentId());
                vo.setMakeBy(configDO.getMakeBy());
                vo.setApartmentName(configDO.getApartmentName());
                vo.setCompanyName(configDO.getCompanyName());
                vo.setCompanyCode(configDO.getCompanyCode());
                vo.setAccountType(configDO.getAccountType());
                vo.setAccountProject1(configDO.getAccountProject1());
                vo.setAccountProjectCode1(configDO.getAccountProjectCode1());
                vo.setAccountProjectName1(configDO.getAccountProjectName1());
                vo.setAccountProject2(configDO.getAccountProject2());
                vo.setAccountProjectCode2(configDO.getAccountProjectCode2());
                vo.setAccountProjectName2(configDO.getAccountProjectName2());
                vo.setAccountProject3(configDO.getAccountProject3());
                vo.setAccountProjectCode3(configDO.getAccountProjectCode3());
                vo.setAccountProjectName3(configDO.getAccountProjectName3());
                vo.setAccountProject4(configDO.getAccountProject4());
                vo.setAccountProjectCode4(configDO.getAccountProjectCode4());
                vo.setAccountProjectName4(configDO.getAccountProjectName4());
                vo.setAccountProject5(configDO.getAccountProject5());
                vo.setAccountProjectCode5(configDO.getAccountProjectCode5());
                vo.setAccountProjectName5(configDO.getAccountProjectName5());
                certificateProjectList.add(vo);
            }
            return certificateProjectList;
        }
        return null;
    }


    /**
     * 保存配置信息
     *
     * @param financeCertificateConfigReqVO
     */
    @Override
    public void save(FinanceCertificateConfigReqVO financeCertificateConfigReqVO) {
        LambdaQueryWrapper<FinanceCertificateConfigDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FinanceCertificateConfigDO::getApartmentId, financeCertificateConfigReqVO.getApartmentId())
                .eq(FinanceCertificateConfigDO::getAccountType, 1);
        //校验参数
        FinanceCertificateConfigDO certificate = financeCertificateConfigMapper.selectOne(lambdaQueryWrapper);
        if (null == certificate) {
            //保存
            saveCertificateArray(financeCertificateConfigReqVO);
            List<FinanceCertificateConfigDO> certificateConfigDOList = financeCertificateConfigMapper.selectList(FinanceCertificateConfigDO::getApartmentId, financeCertificateConfigReqVO.getApartmentId());
            if (certificateConfigDOList.size() > 1) {
                apartmentManageApi.updateApartment(financeCertificateConfigReqVO.getApartmentId(), ApartmentManageEnum.CERTIFICATE_CONFIG.getCode());
            }
        } else {
            //更新
            //删除以前的配置项
            LambdaQueryWrapper<FinanceCertificateConfigDO> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.eq(FinanceCertificateConfigDO::getApartmentId, financeCertificateConfigReqVO.getApartmentId());
            delWrapper.eq(FinanceCertificateConfigDO::getAccountType, 1);
            financeCertificateConfigMapper.delete(delWrapper);
            saveCertificateArray(financeCertificateConfigReqVO);
        }
    }

    /**
     * 保存凭证信息
     *
     * @param financeCertificateConfigReqVO
     */
    private void saveCertificateArray(FinanceCertificateConfigReqVO financeCertificateConfigReqVO) {

        FinanceCertificateConfigDO configDO = new FinanceCertificateConfigDO();
        configDO.setApartmentId(financeCertificateConfigReqVO.getApartmentId());
        configDO.setApartmentName(financeCertificateConfigReqVO.getApartmentName());
        configDO.setCompanyCode(financeCertificateConfigReqVO.getCompanyCode());
        configDO.setCompanyName(financeCertificateConfigReqVO.getCompanyName());
        configDO.setMakeBy(financeCertificateConfigReqVO.getMakeBy());
        configDO.setAccountType(1);
        configDO.setAccountProject1(financeCertificateConfigReqVO.getAccountProject1());
        configDO.setAccountProjectCode1(financeCertificateConfigReqVO.getAccountProjectCode1());
        configDO.setAccountProjectName1(financeCertificateConfigReqVO.getAccountProjectName1());
        configDO.setAccountProject2(financeCertificateConfigReqVO.getAccountProject2());
        configDO.setAccountProjectCode2(financeCertificateConfigReqVO.getAccountProjectCode2());
        configDO.setAccountProjectName2(financeCertificateConfigReqVO.getAccountProjectName2());
        configDO.setAccountProject3(financeCertificateConfigReqVO.getAccountProject3());
        configDO.setAccountProjectCode3(financeCertificateConfigReqVO.getAccountProjectCode3());
        configDO.setAccountProjectName3(financeCertificateConfigReqVO.getAccountProjectName3());
        configDO.setAccountProject4(financeCertificateConfigReqVO.getAccountProject4());
        configDO.setAccountProjectCode4(financeCertificateConfigReqVO.getAccountProjectCode4());
        configDO.setAccountProjectName4(financeCertificateConfigReqVO.getAccountProjectName4());
        configDO.setAccountProject5(financeCertificateConfigReqVO.getAccountProject5());
        configDO.setAccountProjectCode5(financeCertificateConfigReqVO.getAccountProjectCode5());
        configDO.setAccountProjectName5(financeCertificateConfigReqVO.getAccountProjectName5());
        financeCertificateConfigMapper.insert(configDO);

    }


}
